<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
	<th:block th:include="include :: header('修改记账凭证')" />
	<th:block th:include="include :: datetimepicker-css" />
</head>
<body class="gray-bg">
    <div class="main-content">
        <form id="form-pzto-edit" class="form-horizontal"  th:object="${tSrhsPzto}">
            <h4 class="form-header h4"> <span style="color: red; "><label>记账凭证</label></span></h4>
            <input name="pztxh" th:field="*{pztxh}" type="hidden">
            <div class="row">
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label"><span style="color: red; ">*</span>凭证日期：</label>
                        <div class="col-sm-8">
                           <input name="pzrq" id="pzrq"  th:field="*{pzrq}" placeholder="yyyy-MM-dd" class="form-control time-input" type="text" maxlength="30" >
                        </div>
                    </div>
                </div>
              
            	<div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label"><span style="color: red; ">*</span>凭证类型：</label>
                        <div class="col-sm-8"> 
                            <select name="pzlb" id="pzlb" class="form-control" required>
								<option value="1">手工凭证</option>  
							</select> 
                        </div>
                    </div>
                </div>
                
            </div>
            <div class="row">
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label"><span style="color: red; align:center">*</span>凭证编号：</label>
                        <div class="col-sm-8">
                            
                            	<input name="pzbh"  id="pzbh" th:field="*{pzbh}" type="text" placeholder="" class="form-control"  readonly>
                            
                        </div>
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label"><span style="color: red; ">*</span>附件张数：</label>
                        <div class="col-sm-8">
                            <input id="fjzs" name="fjzs"  th:field="*{fjzs}" placeholder="" class="form-control" type="text" maxlength="5" >
                        </div>
                    </div>
                </div>
                
            </div>
		     <div class="container-div">
		        <div class="btn-group-sm" id="toolbar" role="group">
		             <a class="btn btn-success" onclick="insertRow()">
				            <i class="fa fa-plus"></i> 新增行
				        </a>
				        <a class="btn btn-danger " onclick="removeRow()">
				            <i class="fa fa-remove"></i> 删除行
				        </a>
		        </div>
				<div class="row">
					<div class="col-sm-12 select-table ">
						<table id="bootstrap-table"></table>
					</div>
				</div>
				
				<div class="row">
		       <div class="col-sm-4">
		            <div class="form-group">
		                <label class="col-sm-4 control-label"><span style="color: red; ">*</span>制单：</label>
		                <div class="col-sm-8">
		                    <input id="zd" name="zd" placeholder="" class="form-control" type="text"   readonly>
		                </div>
		            </div>
		        </div>
		        <div class="col-sm-4">
		            <div class="form-group">
		                <label class="col-sm-4 control-label"><span style="color: red; ">*</span>审核：</label>
		                <div class="col-sm-8">
		                    <input id="sh" name="sh" placeholder="" class="form-control" type="text"  readonly>
		                </div>
		            </div>
		        </div>
		         <div class="col-sm-4">
		            <div class="form-group">
		                <label class="col-sm-4 control-label"><span style="color: red; ">*</span>状态：</label>
		                <div class="col-sm-8">
		                    <input id="pzzt" name="pzzt" placeholder="" class="form-control" type="text"   readonly>
		                </div>
		            </div>
		        </div>
		    </div>
			</div>
		     
        </form>
    </div>
   
	<th:block th:include="include :: footer" />
	<script th:inline="javascript">
			var startTime = {
		            elem: '#pzrq',
		            format: 'YYYY-MM-DD',
		            //min: laydate.now(), //设定最小日期为当前日期
		            max: laydate.now(), //最大日期
		            istime: false,
		            istoday : true,// 是否显示今天
		            isclear : true, // 是否显示清空
		            issure : true, // 是否显示确认
		        };
		    laydate(startTime);
		    $('#pzrq').val(laydate.now('YYYY-MM-DD'));
			var prefix = ctx + "system/pzto";
			
			$(function() {
		        var options = {
	        		pagination: false,
			        showSearch: false,
	                showRefresh: false,
	                showToggle: false,
	                showColumns: false,
	                sidePagination: "client",
		            columns: [{
			            checkbox: true
			        },
		            {
						field : 'index', 
						title : '序号',
						width:40,
	                    formatter: function (value, row, index) {
	                    	var columnIndex = $.common.sprintf("<input type='hidden' name='index' value='%s' readonly>", $.table.serialNumber(index));
	                    	return columnIndex + $.table.serialNumber(index);
	                    }
					},{
						field : 'zy', 
						width:180,
						title : '摘要',
	                    align: 'left', 
	                    formatter: function(value, row, index) {
			            	var html = $.common.sprintf("<div class='input-group'><input type='text' class='form-control' name='pztis[%s].zy' value='%s'> <span class='input-group-btn'> <button type='button' class='btn btn-primary' onclick='selectZyTree(" + index + ")'>选择 </button> </span></div>", index, value);
			        		return html;
	                    }
	                },
					{
						field : 'kmdm', 
						title : '科目',
						width: 80,
						align: 'center',
			            formatter: function(value, row, index) {
			            	var html = $.common.sprintf("<input class='form-control' type='text' name='pztis[%s].kmdm' value='%s' readonly>", index, value);
			        		return html;
	                    }
					}, {
						field : 'kmmc', 
						title : '科目名称',
						width: 180,
						align: 'center',
	                    formatter: function(value, row, index) {
	                    	var html = $.common.sprintf("<div class='input-group'><input type='text' class='form-control' name='pztis[%s].kmmc' value='%s'> <span class='input-group-btn'> <button type='button' class='btn btn-primary' onclick='selectKmdmTree(" + index + ")'>选择 </button> </span></div>", index, value);
			        		return html;
	                    }
	                },
					{
						field : 'jfje', 
						title : '借方金额',
						align: 'right',
	                    width:150,
			            formatter: function(value, row, index) {
			            	var html = $.common.sprintf("<input class='form-control' type='text' name='pztis[%s].jfje' value='%s'>", index, value);
			        		return html;
	                    }
					},
					{
	                    field: 'dfje',
	                    title: '贷方金额',
	                    align: 'right',
	                    width:150,
			            formatter: function(value, row, index) {
			            	var html = $.common.sprintf("<input class='form-control' type='text' name='pztis[%s].dfje' value='%s'>", index, value);
			        		return html;
	                    }
	                },{
						field : 'fzhsdw', 
						title : '是否核算',
						width:80,
						//visible: false,
			            formatter: function(value, row, index) {
			            	var html = $.common.sprintf("<input class='form-control' type='text' name='pztis[%s].fzhsdw' value='%s' readonly>", index, value);
			        		return html;
	                    }
					},{
						field : 'fzmx', 
						title : '辅助明细',
						width:80,
						//visible: false,
			            formatter: function(value, row, index) {
			            	var html = $.common.sprintf("<input class='form-control' type='text' name='pztis[%s].fzmx' value='%s' readonly>", index, value);
			        		return html;
	                    }
					},{
	                    title: '辅助明细',
	                    align: 'center',
	                    align: 'left',
	                    width: 40,
	                    formatter: function(value, row, index) {
	                        var actions = [];
	                        actions.push('<a class="btn btn-info  btn-xs" href="javascript:void(0)" onclick="addmx(' + index + ')"><i class="fa fa-plus">辅助明细</i></a> ');
	                        actions.push('<a class="btn btn-info  btn-xs" href="javascript:void(0)" onclick="ph(' + index + ')"><i class="fa fa-retweet">平衡</i></a> ');
	                        return actions.join('');
	                    }
	                }]
		        };
		        $.table.init(options);
		        
		        loadData();
		    });
	    	
			function loadData(){
				var pzti=[[${listPzti}]];
				if (pzti!=null){
					for(var i=0;i<pzti.length;i++){
						insertRow();
						var item=pzti[i];
						$("input[name='pztis["+i+"].zy']").val(item['zy']);
						$("input[name='pztis["+i+"].kmdm']").val(item['kmdm']);
						$("input[name='pztis["+i+"].kmmc']").val(item['kmmc']);
		        		$("input[name='pztis["+i+"].jfje']").val(item['jfje']);
		        		$("input[name='pztis["+i+"].dfje']").val(item['dfje']);
		        		var fzmx=item['fzmx'];
		        		if (fzmx==null ||fzmx==undefined || fzmx==''){
		        		   $("input[name='pztis["+i+"].fzhsdw']").val("0");
		        		}else{
		        		   $("input[name='pztis["+i+"].fzhsdw']").val("1");	
		        		}
		        		$("input[name='pztis["+i+"].fzmx']").val(item['fzmx']);
					}
					sub.editColumn();
				}
			}
	    	 
		    function insertRow(){
	          	var count = $("#" + table.options.id).bootstrapTable('getData').length;
	        	sub.editColumn();
	        	
	        	$("#" + table.options.id).bootstrapTable('insertRow', {
	        		index: count,
	                row: {
	                	index: $.table.serialNumber(count),
	                	zy: "",
	                	kmdm: "",
	                	kmmc: "",
	                	jfje: "",
	                	dfje: "",
	                	fzhsdw:"0",
	                	fzmx:""
	                }
	        	});
	            
		    }
		    
		    /* 删除指定表格行 */
		    function removeRow(){
		    	sub.delColumn();
		    }
	    	
		    
		    /*功能科目*/
	        function selectKmdmTree(upIndex) {
	            var options = {
	            	title: '会计科目'+'_'+upIndex,
	                width: "420",
	                height: "400",
	                url: ctx + "system/kmdm/selectKmdmTree/",
	                callBack: dokmdmSubmit
	            };
	            $.modal.openOptions(options);
	        }

	        function dokmdmSubmit(index, layero){ 
	        	var tree = layero.find("iframe")[0].contentWindow.$._tree;
				if ($.tree.notAllowParents(tree)) {
				   var titile=$(".layui-layer-title")[0].innerText;
		     	   var rowIndex=0;
		     	   if (titile!=undefined){
		     		   rowIndex=titile.split('_')[1];
				   var body = layer.getChildFrame('body', index);
	               var kmdm=body.find('#treeId').val();
	               var kmmc=body.find('#treeTitle').val().split('|')[0];
	               var fzhsdw=body.find('#treeTitle').val().split('|')[1];
	          
	               $("input[name='pztis["+rowIndex+"].kmdm']").val(kmdm);
	               $("input[name='pztis["+rowIndex+"].kmmc']").val(kmmc);
	               $("input[name='pztis["+rowIndex+"].fzhsdw']").val(fzhsdw);
	               sub.editColumn();
	               layer.close(index);
				   }
	        	} 
	        }
	        
	        
	        /*摘要*/
	        function selectZyTree(upIndex) {
	            var options = {
	            	title: '会计摘要'+'_'+upIndex,
	                width: "420",
	                height: "400",
	                url: ctx + "system/zyk/selectZykTree/",
	                callBack: dozySubmit
	            };
	            $.modal.openOptions(options);
	        }

	        function dozySubmit(index, layero){
			   var titile=$(".layui-layer-title")[0].innerText;
	     	   var rowIndex=0;
	     	   if (titile!=undefined){
	     		   rowIndex=titile.split('_')[1];
			   var body = layer.getChildFrame('body', index); 
               var zymc=body.find('#treeName').val();
               $("input[name='pztis["+rowIndex+"].zy']").val(zymc);
               sub.editColumn();
               layer.close(index);
			   }
	        }
	        
	        /*功能科目*/
	        function addmx(index) {
	        	var kmdm=$("input[name='pztis["+index+"].kmdm']").val();
	        	var fzhsdw=$("input[name='pztis["+index+"].fzhsdw']").val();
	        	var fzmx=$("input[name='pztis["+index+"].fzmx']").val();
	        	if (kmdm==undefined || kmdm==''|| fzhsdw==undefined||fzhsdw==''||fzhsdw==0) {
	        		return;
	        	}
	        	if (fzmx==undefined){
	        		fzmx="";
	        	}
	        	debugger;
	            var options = {
	            	title: '辅助明细'+'_'+index,
	            	width: "900",
	                height: "500",
	                url: prefix + "/addmx/"+index+'/'+kmdm+'/'+fzhsdw,
	                callBack: fzmxSubmit
	            };
	            $.modal.openOptions(options);
	        }
	        function fzmxSubmit(index, layero){
	        	var rows = layero.find("iframe")[0].contentWindow.$("#mxtableId").bootstrapTable('getData');
	        	if(rows.length == 0){
	        		 layer.alert("<font color='red'>请录入辅助信息</font>", {
	        		        icon: 0,
	        		        title: "系统提示"
	        		 })
	        		 return;	
	        	}
	        	var dateIds = new Array();
	        	var itemje=0;
	        	$.each(rows, function (key, value) {
                    if (value.dwbm==''){
                   	 dateIds.push("1");
                   	 return false; 
                    }
                    if (value.je<=0){
                   	 dateIds.push("1");
                   	 return false; 
                    }
                    itemje=itemje+Number(value.je);
                });
	        	if(dateIds.length>0){
	        		 layer.alert("<font color='red'>辅助明细单位信息为空或金额小于等0!</font>", {
	        		        icon: 0,
	        		        title: "系统提示"
	        		 })
	        		 return;
	        	}
	        	var titile=$(".layui-layer-title")[0].innerText;
		     	var rowIndex=0;
		     	if (titile!=undefined){
		     		   rowIndex=titile.split('_')[1];
		     	}
		    	var jfje=$("input[name='pztis["+rowIndex+"].jfje']").val();
		    	var dfje=$("input[name='pztis["+rowIndex+"].dfje']").val();
		    	var jfje=Number(jfje);
		    	var dfje=Number(dfje);
		    	if (eval(jfje)!=eval(itemje) &&  eval(dfje)!=eval(itemje)){
		    		 layer.alert("<font color='red'>辅助明细金额与科目金额不相等!</font>", {
	        		        icon: 0,
	        		        title: "系统提示"
	        		 })
	        		 return;
		    	}
	        	var json=JSON.stringify(rows);
	        	$("input[name='pztis["+rowIndex+"].fzmx']").val(json);
	        	sub.editColumn();
       			layer.close(index);
	        }
	        
	        
	        function submitHandler() {
	        	sub.editColumn();
	            if ($.validate.form()) {
	            	var data = $("#form-pzto-edit").serializeArray();
	             	var count = $("#" + table.options.id).bootstrapTable('getData').length;
	             	if  (count<2){
	             		 layer.alert("<font color='red'>会计分录至少为2条</font>", {
		        		        icon: 0,
		        		        title: "系统提示"
		        		 })
		        		 return;
	             	}
	            	var dateIds = new Array();
	            	var datejdfjes = new Array();
	            	var datafzhsmxs = new Array();
		        	var jfjes=0;
		        	var dfjes=0;
		        	for (var i=0;i<count;i++){
		        		var kmdm=$("input[name='pztis["+i+"].kmdm']").val();
		        		var jfje=$("input[name='pztis["+i+"].jfje']").val();
		        		var dfje=$("input[name='pztis["+i+"].dfje']").val();
		        		var fzhsdw=$("input[name='pztis["+i+"].fzhsdw']").val();
		        		var fzmx=$("input[name='pztis["+i+"].fzmx']").val();
	                    if (kmdm==''){
		                   	 dateIds.push("1");
	                    }
	                    if (jfje!='' && dfje!=''&& jfje!='0' && dfje!='0'){
	                    	 datejdfjes.push("1");
	                    }
	                    if (jfje!=''){
	                    	jfjes=jfjes+Number(jfje);
	                    }
	                    if (dfje!=''){
	                   	   dfjes=dfjes+Number(dfje);
	                    }
	                    if (fzhsdw!='0'){
	                    	if (fzmx==''){
	                    		datafzhsmxs.push("1");
	                    	}
	                    }
	                }
		        	if(dateIds.length>0){
		        		 layer.alert("<font color='red'>科目为空</font>", {
		        		        icon: 0,
		        		        title: "系统提示"
		        		 })
		        		 return;
		        	}
		        	if(datejdfjes.length>0){
		        		 layer.alert("<font color='red'>借贷方金额问题</font>", {
		        		        icon: 0,
		        		        title: "系统提示"
		        		 })
		        		 return;
		        	}
		        	if(datafzhsmxs.length>0){
		        		 layer.alert("<font color='red'>需要辅助核算科目没有辅助明细信息</font>", {
		        		        icon: 0,
		        		        title: "系统提示"
		        		 })
		        		 return;
		        	}
		        	if (eval(jfjes)!=eval(dfjes)){
		        		layer.alert("<font color='red'>借贷金额不符</font>", {
	        		        icon: 0,
	        		        title: "系统提示"
	        		 	})
	        		  return;
		        	}
		        	var kmdmItem = new Array();
		        	for (var i=0;i<count;i++){
		        		var zy=$("input[name='pztis["+i+"].zy']").val();
		        		var kmdm=$("input[name='pztis["+i+"].kmdm']").val();
		        		var kmmc=$("input[name='pztis["+i+"].kmmc']").val();
		        		var jfje=$("input[name='pztis["+i+"].jfje']").val();
		        		var dfje=$("input[name='pztis["+i+"].dfje']").val();
		        		var fzhsdw=$("input[name='pztis["+i+"].fzhsdw']").val();
		        		var fzmx="";
		        		if (fzhsdw=='0' || fzhsdw==0){
		        			fzmx="";
		        		}else{
		        			fzmx=$("input[name='pztis["+i+"].fzmx']").val();	
		        		}
		        		var pztixx={"zy":zy,"kmdm":kmdm,"kmmc":kmmc,"jfje":jfje,"dfje":dfje,"fzhsdw":fzhsdw,"fzmx":fzmx};
		        		kmdmItem.push(pztixx);
		        	}
	            	var jsonkmdmItem=JSON.stringify(kmdmItem);
		        	data.push({"name": "params[pzti]", "value": jsonkmdmItem});
	                $.operate.save(prefix + "/add", data);
	                
		        	
	            }
	        }
	        function ph(index) {
	        	sub.editColumn();
             	var count = $("#" + table.options.id).bootstrapTable('getData').length;
             	if  (count<2){
	        		 return false;
             	}
	        	var jfjes=0;
	        	var dfjes=0;
	        	for (var i=0;i<count;i++){
	        		var jfje=$("input[name='pztis["+i+"].jfje']").val();
	        		var dfje=$("input[name='pztis["+i+"].dfje']").val();
	        		
                    if (jfje!=''){
                    	jfjes=jfjes+Number(jfje);
                    }
                    if (dfje!=''){
                   	   dfjes=dfjes+Number(dfje);
                    }
                } 
	        	var cha = Number(jfjes-dfjes);
	        	if (cha!=0){
		        	var curjf=$("input[name='pztis["+index+"].jfje']").val();
		        	var curdf=$("input[name='pztis["+index+"].dfje']").val();
		        	var curjfje=0;
		        	var curdfje=0;
		        	if (curjf!=''){
		        		curjfje=Number(curjf);
                    }
		        	if (curdf!=''){
		        		curdfje=Number(curdf);
                    }
		        	var phje=cha-(curjfje-curdfje);
		        	$("input[name='pztis["+index+"].jfje']").val("0");
		        	$("input[name='pztis["+index+"].dfje']").val(phje.toString());
		        	sub.editColumn();
	        	}
	        }
    </script>
</body>
</html>
